package com.powernode.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powernode.config.WhiteUrlsConfig;
import com.powernode.constant.AuthConstants;
import com.powernode.constant.HttpConstants;
import com.powernode.enums.BusinessEnum;
import com.powernode.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 全局token过滤器
 * 前后端约定好令牌存放的位置：请求头的Authorization bearer token
 *
 * @author ZhangYu
 * @date 2024/11/24 1:52
 */
@Slf4j
@Component
public class AuthFilter implements GlobalFilter, Ordered {


    @Resource
    private WhiteUrlsConfig whiteUrlsConfig;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 检验token
     * 1、获取请求路径
     * 2、判断请求路径是否可以放行
     * 放行：不需要验证身份
     * 不放行：需要验证身份
     *
     * @param serverWebExchange  HTTP请求和响应的上下文
     * @param gatewayFilterChain 网关过滤链表
     * @return Mono<Void>表示一个不返回任何值的异步操作
     */
    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
        //获取请求对象
        ServerHttpRequest serverHttpRequest = serverWebExchange.getRequest();
        //获取请求路径
        String path = serverHttpRequest.getPath().toString();
        //判断当前请求路径是否需要放行，是否存在于白名单中
        boolean isContains = whiteUrlsConfig.getAllowUrls().contains(path);
        if (isContains) {
            //包含：请求路径包含在白名单中，即需要放行
            return gatewayFilterChain.filter(serverWebExchange);
        }
        //请求路径不在白名单中，需要对其进行身份的验证
        //从约定好的位置获取Authorization的值，值的格式为：bearer token
        String authorizationValue = serverHttpRequest.getHeaders().getFirst(AuthConstants.AUTHORIZATION);
        //判断是否有值
        if (StringUtils.hasText(authorizationValue)) {
            //从Authorization的值中获取token
            String tokenValue = authorizationValue.replaceFirst(AuthConstants.BEARER, "");
            //判断token值是否在redis中存在
            if (StringUtils.hasText(tokenValue) && Boolean.TRUE.equals(stringRedisTemplate.hasKey(AuthConstants.LOGIN_TOKEN_PREFIX + tokenValue))) {
                //身份验证通过，放行
                return gatewayFilterChain.filter(serverWebExchange);
            }
        }
        //流程如果走到这：说明验证身份没有通过或请求不合法
        log.error("------------------------------------------------>拦截非法请求，时间{}，请求API路径：{}", new Date(), path);
        //获取响应对象
        ServerHttpResponse serverHttpResponse = serverWebExchange.getResponse();
        //设置响应头信息
        serverHttpResponse.getHeaders().set(HttpConstants.CONTENT_TYPE, HttpConstants.APPLICATION_JSON);
        //设置响应消息
        Result<Object> result = Result.fail(BusinessEnum.UN_AUTHORIZATION.getCode(), BusinessEnum.UN_AUTHORIZATION.getDesc());
        //创建一个objectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] bytes;
        try {
            bytes = objectMapper.writeValueAsBytes(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        DataBuffer dataBuffer = serverHttpResponse.bufferFactory().wrap(bytes);
        return serverHttpResponse.writeWith(Mono.just(dataBuffer));
    }

    @Override
    public int getOrder() {
        return -5;
    }

}
